//package com.travel.interceptor;
//
//import cn.hutool.core.map.MapUtil;
//import cn.hutool.core.util.ObjectUtil;
//import com.travel.context.UserContext;
//import com.travel.exception.BaseException;
//import com.travel.service.impl.TokenService;
//import com.travel.utils.StringUtils;
//import io.jsonwebtoken.Claims;
//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.servlet.http.HttpServletResponse;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.ObjectUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.web.method.HandlerMethod;
//import org.springframework.web.servlet.HandlerInterceptor;
//
///**
// * @author: libin
// * Date: 2025/6/10 8:18
// * Description:
// **/
//@Component
//@Slf4j
//public class MemberLoginInterceptor implements HandlerInterceptor {
//
//    @Autowired
//    private TokenService tokenService;
//
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        log.info("请求被拦截: {}", request.getRequestURL().toString());
//
//        // 如果不是映射到方法，直接通过
//        if (!(handler instanceof HandlerMethod)) {
//            return true;
//        }
//
//        // 跨域预检请求（OPTIONS方法）直接放行
//        if ("OPTIONS".equals(request.getMethod())) {
//            return true;
//        }
//
//        //获取token
//        String token = request.getHeader("authorization");
//        if(StringUtils.isEmpty(token)){
//            throw new BaseException("认证失败");
//        }
//        //解析token
//        Claims claims = tokenService.parseToken(token);
//        if(ObjectUtil.isEmpty(claims)){
//            throw new BaseException("认证失败");
//        }
//        //获取token中的数据
//        Integer userId = MapUtil.get(claims, "userId", Integer.class);
//        if(ObjectUtils.isEmpty(userId)){
//            throw new BaseException("认证失败");
//        }
//        //存储到threadLocal中
//        UserContext.setCurrentId(userId);
//        return true;
//    }
//
//    @Override
//    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//        //清理threadLocal中存储的数据
//        UserContext.removeCurrentId();
//    }
//}
